package com.zw.cloud.mybatis.plus.service.impl.stenter;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.zw.cloud.mybatis.plus.entity.stenter.BaseShapingParamConfig;
import com.zw.cloud.mybatis.plus.mapper.stenter.BaseShapingParamConfigMapper;
import com.zw.cloud.mybatis.plus.service.api.stenter.IBaseShapingParamConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 参数配置表 服务实现类
 * </p>
 *
 * @author zw
 * @since 2023-12-04
 */
@Service
public class BaseShapingParamConfigServiceImpl extends ServiceImpl<BaseShapingParamConfigMapper, BaseShapingParamConfig> implements IBaseShapingParamConfigService {

    @Override
    public void initSort(int type) {
        LambdaQueryWrapper<BaseShapingParamConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseShapingParamConfig::getType,type)
                .eq(BaseShapingParamConfig::getOrgCode,"devController").orderByAsc(BaseShapingParamConfig::getSort);
        List<BaseShapingParamConfig> configList = baseMapper.selectList(queryWrapper);
        for (int i = 0; i < configList.size(); i++) {
            BaseShapingParamConfig baseShapingParamConfig = configList.get(i);
            baseShapingParamConfig.setSort(i + 1);
            updateById(baseShapingParamConfig);
        }
    }

    @Override
    public String getInitSortSql(int type) {
        LambdaQueryWrapper<BaseShapingParamConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseShapingParamConfig::getType,type)
                .eq(BaseShapingParamConfig::getOrgCode,"devController")
                .orderByAsc(BaseShapingParamConfig::getSort);
        List<BaseShapingParamConfig> configList = baseMapper.selectList(queryWrapper);
        // 遍历 configList,组装sql语句，根据type和name修改sort字段
        StringBuilder stringBuilder = new StringBuilder();
        for (BaseShapingParamConfig baseShapingParamConfig : configList) {
            // 拼接sql语句
            stringBuilder.append("update base_shaping_param_config set sort = ")
                    .append(baseShapingParamConfig.getSort())
                    .append(" where type = ").append(type).append(" and name = '").append(baseShapingParamConfig.getName()).append("' ; \n ");

        }
        System.out.println(stringBuilder.toString());
        return stringBuilder.toString();
    }

    /**
     * 定前布车号、定后布车号、上机门幅、落机门幅、定后克重、实际数量、助剂填写、定前克重，默认“是”；
     * 历史工厂 将上述字段 status 设置为1，其他字段取值为  对应工艺参数是否启用
     */
    @Override
    public void updateStatusByOtherType() {
        Set<String> openSet = Sets.newHashSet("preCarNumber", "carNumber", "upperDoorWidth", "lowerDoorWidth", "gramHeft", "actualQuantity", "assistant", "schedulePreWeight");
        LambdaQueryWrapper<BaseShapingParamConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(BaseShapingParamConfig::getType, Lists.newArrayList(3, 13))
                .eq(BaseShapingParamConfig::getOrgCode, "devController");
        List<BaseShapingParamConfig> configList = baseMapper.selectList(queryWrapper);
        Map<Integer, List<BaseShapingParamConfig>> typeGroup = configList.stream().collect(Collectors.groupingBy(BaseShapingParamConfig::getType));
        List<BaseShapingParamConfig> craftConfigList = typeGroup.get(13);
        List<BaseShapingParamConfig> needUpdateList = typeGroup.get(3);
        if (CollectionUtils.isEmpty(craftConfigList) || CollectionUtils.isEmpty(needUpdateList)) {
            return;
        }
        Map<String, Integer> thirteenMap = craftConfigList.stream().collect(Collectors.toMap(BaseShapingParamConfig::getName, BaseShapingParamConfig::getStatus));
        for (BaseShapingParamConfig baseShapingParamConfig : needUpdateList) {
            String name = baseShapingParamConfig.getName();
            Integer status;
            if (openSet.contains(name)) {
                status = 1;
            } else if (StringUtils.equals("weftDensity", name)) {
                status = thirteenMap.get("scheduleWeftDensity");
            } else {
                status = thirteenMap.get(name);
            }
            baseShapingParamConfig.setStatus(status);
        }
        updateBatchById(needUpdateList);
    }

    @Override
    public List<BaseShapingParamConfig> queryListByType(int type) {
        LambdaQueryWrapper<BaseShapingParamConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseShapingParamConfig::getType,type).orderByAsc(BaseShapingParamConfig::getSort);
        return baseMapper.selectList(queryWrapper);
    }
}
